package controllers;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
import AlleSellerSecurity.SecurityProfile;
import gui.frameNames;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TableView;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

/**
 *
 * @author lewuaza
 */
public class MainController implements Initializable {
    //FXML Members
    @FXML
    private Label welcomeLabel;
    @FXML
    private Button exitButton;
    @FXML
    private Button allegroAccountSettings;
    @FXML 
    private Button payUButton;
    @FXML
    private Button manageUserButton;
    @FXML
    private Button manageApplicationButton;
    @FXML 
    private Button licenseButton;
    @FXML 
    private Button addAuctionButton;
    @FXML
    private Button editAuctionButton;
    @FXML
    private Button showAuctionsButton;
    @FXML
    private Button editTransactionButton;
    @FXML
    private StackPane tableAnchor;
    @FXML
    private Button showTransactionButton;
    @FXML 
    private Button importBankButton;
    @FXML
    private Button showClients;
    @FXML
    private Button editClient;
    @FXML
    private Button showReportsButton;
    @FXML
    private Button addReportButton;
    @FXML
    private Button editReportButton;
    @FXML
    private Button planReportButton;
    //Class Members
    private SecurityProfile profile;
    private Parent root;
    
    @FXML void handleAddReportAction(ActionEvent event)
    {
        this.showSimpleWindow(frameNames.addReportTemplateForm, 600, 294);
    }

    @FXML void handleEditReportAction(ActionEvent event)
    {
        this.showSimpleWindow(frameNames.editReportTemplateForm, 600, 294);
    }

        @FXML void handlePlanReportAction(ActionEvent event)
    {
        this.showSimpleWindow(frameNames.planReportForm, 600, 301);
    }
    
    @FXML
    private void handleShowReportsAction(ActionEvent event)
    {
        this.loadFXMLToAnchorPane(tableAnchor, frameNames.reportsLayoutForm);
        
    }
    
    @FXML
    private void handleShowClientAction(ActionEvent event)
    {
        this.loadFXMLToAnchorPane(tableAnchor, frameNames.showClientsForm);
    }
    
    @FXML
    private void handleEditClientAction(ActionEvent event)
    {
        this.showSimpleWindow(frameNames.editClientForm, 600, 400);    
    }
    
    @FXML
    private void handleImportBankAction(ActionEvent event)
    {
        this.showSimpleWindow(frameNames.bankImportForm, 223, 164);
    }
    
    @FXML
    private void handleShowTransactionAction(ActionEvent event)
    {
        this.loadFXMLToAnchorPane(tableAnchor, frameNames.showTransactionForm);    
    }
    
    @FXML 
    private void handleEditTransactionAction(ActionEvent event)
    {
        this.showSimpleWindow(frameNames.editTransactionForm, 346, 226);
    }
    
    @FXML 
    private void handleShowAuctionsAction(ActionEvent event)
    {
        this.loadFXMLToAnchorPane(tableAnchor, frameNames.showAuctionsForm);
    }
    @FXML
    private void handleEditAuctionAction(ActionEvent event)
    {
        this.showSimpleWindow(frameNames.addAuctionForm, 600, 400);
    }
    
    @FXML
    private void handleAddAuctionAction(ActionEvent event)
    {
        this.showSimpleWindow(frameNames.addAuctionForm, 600, 400);
    }
    @FXML
    private void handleLicenseAction(ActionEvent event)
    {
        this.showSimpleWindow(frameNames.licenseForm, 227, 143);
    }
    
    @FXML
    private void handleExitAction(ActionEvent event) {
        Stage stage = (Stage) exitButton.getScene().getWindow();
        stage.close();
    }
    
    @FXML
    private void handleManageApplicationAction(ActionEvent event)
    {
        this.showSimpleWindow("/gui/app settings.fxml", 600, 400);
    }
    @FXML
    private void handleManageUserAction(ActionEvent event)
    {
        try {
            FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/gui/zarzadzaj uzytkownikammi.fxml"));
            
            root = (Parent) fxmlLoader.load();
            
            Stage stage = new Stage();
            stage.setTitle("AlleSeller");
            stage.setScene(new Scene(root, 600, 400));
            
            stage.show();
        } catch (IOException ex) {
            Logger.getLogger(MainController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    @FXML
    private void handlePayUAction(ActionEvent event)
    {
        try {
            FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/gui/PayU.fxml"));
            
            root = (Parent) fxmlLoader.load();
            
            Stage stage = new Stage();
            stage.setTitle("AlleSeller");
            stage.setScene(new Scene(root, 238, 136));
            
            stage.show();
        } catch (IOException ex) {
            Logger.getLogger(MainController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    @FXML
    private void handleAllegroAccountSettingsAction(ActionEvent event) {
        try {
            FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/gui/Administracja konto.fxml"));
            
            root = (Parent) fxmlLoader.load();
            
            Stage stage = new Stage();
            stage.setTitle("AlleSeller");
            stage.setScene(new Scene(root, 466, 293));
            
            stage.show();
        } catch (IOException ex) {
            Logger.getLogger(MainController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {
    }

    void initData(SecurityProfile profile) {
        this.profile = profile;
        this.welcomeLabel.setText("Witaj " + profile.getLogin() + "!");
    }
    /**
     * Wczytuje dany FXML do StackPane
     * @param target StackPane gdzie ma byc wczytany FXML
     * @param name FXML do wczytania
     */
    private void loadFXMLToAnchorPane(StackPane target,String name)
    {
        try {   
            TableView pane = (TableView) FXMLLoader.load(getClass().getResource(name));
            target.getChildren().setAll(pane);
        } catch (IOException ex) {
            Logger.getLogger(MainController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    /**
     * Wczytuje podstawowe okienko w okreslonym rozmiarze , bez dodatkowych bajerow typu inicjalizacja danych itp
     * @param name nazwa okna
     * @param width szerokosc okna
     * @param height wysokosc okna
     */
    private void showSimpleWindow(String name,int width,int height)
    {
        try {
            FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource(name));
            
            root = (Parent) fxmlLoader.load();
            
            Stage stage = new Stage();
            stage.setTitle("AlleSeller");
            stage.setScene(new Scene(root, width, height));
            
            stage.show();
        } catch (IOException ex) {
            Logger.getLogger(MainController.class.getName()).log(Level.SEVERE, null, ex);
        }    
    }
}
